package com.yong.android.framework.view.recyclerview;

import android.support.annotation.IntRange;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

public abstract class RecyclerViewCell<VH extends RecyclerView.ViewHolder> {
    CellListAdapter mHostAdapter;
    String name;

    protected abstract VH onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);

    protected abstract void onBindViewHolder(VH holder, int position);

    public abstract int getCount();

    @IntRange(from = 1)
    public int getSpan(int totalSpan, int position) {
        return 1;
    }

    public final void notifyCenterDataSetChanged() {
        if (mHostAdapter==null){
            return;
        }
        mHostAdapter.notifyDataSetChanged();

    }

    @IntRange(from = -127, to = 127)
    public int getItemViewType(int position) {
        return 0;
    }


    public final void notifyItemChanged() {
        notifyItemChangedRange(0, getCount());
    }

    public final void notifyItemChanged(int position) {
        notifyItemChangedRange(position, 1);
    }

    public final void notifyItemChangedRange(int position, int itemCount) {
        if (mHostAdapter==null){
            return;
        }
        mHostAdapter.notifyItemRangeChanged(findCellAdapterPosition() + position, itemCount);
    }

    public final void notifyItemInserted(int position) {
        notifyItemRangeInserted(position, 1);
    }

    public final void notifyItemRangeInserted(int position, int itemCount) {
        if (mHostAdapter==null){
            return;
        }
        mHostAdapter.notifyItemRangeInserted(findCellAdapterPosition() + position, itemCount);

    }

    public final void notifyItemRemoved(int position) {
        notifyItemRangeRemoved(position, 1);
    }

    public final void notifyItemRangeRemoved(int position, int itemCount) {
        if (mHostAdapter==null){
            return;
        }
        mHostAdapter.notifyItemRangeRemoved(findCellAdapterPosition() + position, itemCount);
    }


    private int findCellAdapterPosition() {
        if (mHostAdapter == null) {
            return -1;
        }
        return mHostAdapter.findCellAdapterPosition(this);
    }


    public final int findCellPosition() {
        if (mHostAdapter == null) {
            return -1;
        }
        return mHostAdapter.findCellPosition(this);
    }

}
